import axios from "axios"
import toast from "vant/lib/toast/index.js"

const req = axios.create({
  baseURL: `/`
})

req.interceptors.request.use(
  (config) => {
    config.headers.token = localStorage.getItem("token")
    return config
  },
  (error) => {
    return Promise.reject(error)
  }
)

req.interceptors.response.use((res) => {
  // 判断是否有返回码（1001开头都是token的错误）
  if (res.data.code) {
    if (String(res.data.code).indexOf("1001") === 0) {
      toast(res.data.msg)
    }
  }
  return res.data
})

export default req
